package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.Component;

import javax.swing.SwingConstants;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import java.awt.Container;
import java.awt.Color;

public class TryEx extends JFrame {

	private JPanel Calculator;
	private JPanel contentPane;
	private JTextField su1;
	private JTextField su2;
	private JTextField result;
	private JButton btnNewButton;
	private JLabel lblResult;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JButton btnMultiply;
	private JButton btnPlus;
	private JButton btnMinus;
	private JMenuBar menuBar;
	private JButton btnNewButton_1;
	private JTextField result2;
	private JMenu mnMenu;
	private JMenuItem mntmCalculator;
	private JMenuItem mntmMiniGame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					TryEx frame = new TryEx();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public TryEx() {
		setTitle("Divide Program");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 430);
		setJMenuBar(getMenuBar_1());
		Calculator = new JPanel();
		Calculator.setBackground(Color.PINK);
		Calculator.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(Calculator);
		Calculator.setLayout(null);
		Calculator.add(getSu1());
		Calculator.add(getSu2());
		Calculator.add(getResult());
		Calculator.add(getLblResult());
		Calculator.add(getLblNewLabel());
		Calculator.add(getLblNewLabel_1());
		Calculator.add(getBtnNewButton());
		Calculator.add(getBtnMultiply());
		Calculator.add(getBtnPlus());
		Calculator.add(getBtnMinus());
		Calculator.add(getBtnNewButton_1());
		Calculator.add(getResult2());
	}
	private JTextField getSu1() {
		if (su1 == null) {
			su1 = new JTextField();
			su1.setBounds(81, 11, 116, 21);
			su1.setColumns(10);
		}
		return su1;
	}
	private JTextField getSu2() {
		if (su2 == null) {
			su2 = new JTextField();
			su2.setColumns(10);
			su2.setBounds(81, 42, 116, 21);
		}
		return su2;
	}
	private JTextField getResult() {
		if (result == null) {
			result = new JTextField();
			result.setColumns(10);
			result.setBounds(81, 73, 116, 21);
		}
		return result;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("Divide");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try{
						compute();
					}catch(Exception ex){						
						result.setText("Error~ :)");
						result2.setText("Don't put strings in the num1, num2");
					}
					
				}
			});
			btnNewButton.setBounds(209, 10, 77, 53);
		}
		return btnNewButton;
	}
	private JButton getBtnMultiply() {
		if (btnMultiply == null) {
			btnMultiply = new JButton("Multiply");
			btnMultiply.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					double s1 = Double.parseDouble(su1.getText());
					double s2 = Double.parseDouble(su2.getText());
					double r = s1*s2;
					
					result.setText(""+r);
				}
			});
			btnMultiply.setBounds(298, 11, 77, 53);
		}
		return btnMultiply;
	}
	private JButton getBtnPlus() {
		if (btnPlus == null) {
			btnPlus = new JButton("Plus");
			btnPlus.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					double s1 = Double.parseDouble(su1.getText());
					double s2 = Double.parseDouble(su2.getText());
					double r = s1+s2;
					
					result.setText(""+r);
				}
			});
			btnPlus.setBounds(209, 72, 77, 53);
		}
		return btnPlus;
	}
	private JButton getBtnMinus() {
		if (btnMinus == null) {
			btnMinus = new JButton("Minus");
			btnMinus.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					double s1 = Double.parseDouble(su1.getText());
					double s2 = Double.parseDouble(su2.getText());
					double r = s1-s2;
					
					result.setText(""+r);
				}
			});
			btnMinus.setBounds(298, 72, 77, 53);
		}
		return btnMinus;
	}
	private JLabel getLblResult() {
		if (lblResult == null) {
			lblResult = new JLabel("Result");
			lblResult.setHorizontalAlignment(SwingConstants.RIGHT);
			lblResult.setBounds(12, 76, 57, 15);
		}
		return lblResult;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("Num2");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setBounds(12, 45, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("Num1");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1.setBounds(12, 14, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnMenu());
		}
		return menuBar;
	}
	public void compute(){
		double s1 = Double.parseDouble(su1.getText());
		double s2 = Double.parseDouble(su2.getText());
		
		double r = s1/s2;
		
		result.setText(""+r);
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("Make Exception");
			btnNewButton_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					Exception e = new Exception("This exception made by Hacker :)");
					try{
						throw e;
					}catch(Exception ex){
						result2.setText(ex.toString());
					}finally{
						result2.setText("Time is gold");
					}
				}
			});
			btnNewButton_1.setBounds(209, 135, 163, 23);
		}
		return btnNewButton_1;
	}
	private JTextField getResult2() {
		if (result2 == null) {
			result2 = new JTextField();
			result2.setHorizontalAlignment(SwingConstants.CENTER);
			result2.setText("Make Some Exceptions :)");
			result2.setBounds(12, 168, 360, 193);
			result2.setColumns(10);
		}
		return result2;
	}
	private JMenu getMnMenu() {
		if (mnMenu == null) {
			mnMenu = new JMenu("Menu");
			mnMenu.add(getMntmCalculator());
			mnMenu.add(getMntmMiniGame());
		}
		return mnMenu;
	}
	private JMenuItem getMntmCalculator() {
		if (mntmCalculator == null) {
			mntmCalculator = new JMenuItem("Calculator");
			mntmCalculator.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					setContentPane(Calculator);
					
				}
			});
		}
		return mntmCalculator;
	}
	private JMenuItem getMntmMiniGame() {
		if (mntmMiniGame == null) {
			mntmMiniGame = new JMenuItem("Mini Game");
			mntmMiniGame.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					MiniGame mg = new MiniGame();				
					
					setContentPane(mg);
					
					/*contentPane.add(mg);*/
					
				}
			});
		}
		return mntmMiniGame;
	}
}
